package com.lsh.msgpack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.msgpack.MessagePack;

/**
 * @author ：LiuShihao
 * @date ：Created in 2021/4/12 2:36 下午
 * @desc ：MessagePack 编码器
 *
 * MsgpackEncoder 继承 MessageToByteEncoder ，它阿静负责将Object类型的Java对象 编码为byte数组，然后写入到ByteBuf中
 */
public class MsgpackEncoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        MessagePack msgpack = new MessagePack();
        byte[] raw = msgpack.write(o);
        byteBuf.writeBytes(raw);
    }
}
